import { COUPON_OFFER_TYPE, COUPON_DATE_TYPE } from '@constants/market';
import { formatDate } from '@common/format';
const DATE_FORMAT = 'YYYY.MM.DD';

const dealDiscountToCoupon = (item) => {
    const { id, name, price, offer_type, quota, use_period_type, use_begined,
        use_ended, available_after_got_days, days } = item;
    const isDiscount = offer_type == COUPON_OFFER_TYPE.discount; // 是否是折扣
    let valueTip = '';
    let couponType = '';
    let couponText = '';
    switch(offer_type) {
    case COUPON_OFFER_TYPE.discount:
        couponType = '折扣券';
        couponText = `${price / 10}折`;
        valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
        break;
    case COUPON_OFFER_TYPE.limit:
        couponType = '特价券';
        couponText = `特价${price / 100}`;
        valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
        break;
    case COUPON_OFFER_TYPE.cut:
        couponType = '减价券';
        couponText = `减${price / 100}`;
        valueTip = quota > 0 ? `满${quota / 100}可用` : '无门槛';
        break;
    case COUPON_OFFER_TYPE.quotaCut:
        couponType = '满减券';
        couponText = `${quota / 100}减${price / 100}`;
        valueTip = quota > 0 ? `满${quota / 100}减${price / 100}` : '无门槛';
        break;
    }
    let validString = '';
    const _arr = ['当日', '次日'];
    switch(use_period_type) {
    // 固定日期
    case COUPON_DATE_TYPE.fixed:
        validString = `${formatDate(use_begined, DATE_FORMAT)} - ${formatDate(use_ended, DATE_FORMAT)}`;
        break;
    // 领取后生效
    case COUPON_DATE_TYPE.after:
        validString = available_after_got_days > 1 ? `领取后${available_after_got_days}日生效` : `${_arr[available_after_got_days]}生效`;
        validString += `，${days}天有效`;
        break;
    }
    return {
        id, // id
        name, // 名字
        value: price, // 优惠金额或优惠折扣
        valueTip, // 优惠提示
        isDiscount, // 是否是折扣
        validString, // 有效期
        couponType, // 优惠券类型
        couponText, // 优惠券提示文字
    };
};
const dealDiscountToActivity = (item) => {
    const { id, name, offer_type, quota, price } = item;
    let valueTip = '';
    switch(offer_type) {
    case COUPON_OFFER_TYPE.discount:
        valueTip = `${price / 10}折`;
        break;
    case COUPON_OFFER_TYPE.limit:
        valueTip = `特价${price / 100}`;
        break;
    case COUPON_OFFER_TYPE.cut:
        valueTip = `减${price / 100}`;
        break;
    case COUPON_OFFER_TYPE.quotaCut:
        valueTip = `${quota / 100}减${price / 100}`;
        break;
    }
    return {
        id,
        name,
        valueTip,
    };
};

/**
 * 处理优惠信息
 * @param {Array} list 优惠信息
 */
export const dealDiscountInfo = (list) => {
    const activityList = [];
    const couponList = [];
    list.forEach(item => {
        const { nature } = item;
        if (nature === 'voucher') {
            couponList.push(dealDiscountToCoupon(item));
        }
        if (nature === 'items_off' || nature === 'item_off') {
            activityList.push(dealDiscountToActivity(item));
        }
    });
    return {
        activityList,
        couponList,
    };
};
